package cn.ilovese.collect.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;

@Configuration
public class DruidSourceConfig {

    // 有多数据源时可调为多数据源的配置，如@Value("${spring.datasource.main.name}")
    @Value("${spring.datasource.name}")
    private String mainName;

    @Value("${spring.datasource.url}")
    private String mainUrl;

    @Value("${spring.datasource.username}")
    private String mainUsername;

    @Value("${spring.datasource.password}")
    private String mainPassword;

    @Value("${spring.datasource.driver-class-name}")
    private String mainDriverClassName;

    @Value("${spring.datasource.initialSize}")
    private String mainInitialSize;

    @Value("${spring.datasource.minIdle}")
    private String mainMinIdle;

    @Value("${spring.datasource.maxActive}")
    private String mainMaxActive;

    @Value("${spring.datasource.maxWait}")
    private String mainMaxWait;

    @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
    private String mainTimeBetweenEvictionRunsMillis;

    @Value("${spring.datasource.minEvictableIdleTimeMillis}")
    private String mainMinEvictableIdleTimeMillis;

    @Value("${spring.datasource.validationQuery}")
    private String mainValidationQuery;

    @Value("${spring.datasource.filters}")
    private String mainFilters;

    @Value("{spring.datasource.logSlowSql}")
    private String mainLogSlowSql;

    @Value("${spring.datasource.type}")
    private String mainType;

    @Value("{spring.datasource.maxIdle}")
    private String mainMaxIdle;

    @Bean
    public DataSource mainDataSource() {
        DruidDataSource datasource = new DruidDataSource();

        datasource.setUrl(mainUrl);
        datasource.setUsername(mainUsername);
        datasource.setPassword(mainPassword);
        datasource.setDriverClassName(mainDriverClassName);
        if (StringUtils.isNotBlank(mainInitialSize)) {
            datasource.setInitialSize(Integer.parseInt(mainInitialSize));
        }
        if (StringUtils.isNotBlank(mainMinIdle)) {
            datasource.setMinIdle(Integer.parseInt(mainMinIdle));
        }
        if (StringUtils.isNotBlank(mainMaxActive)) {
            datasource.setMaxActive(Integer.parseInt(mainMaxActive));
        }
        if (StringUtils.isNotBlank(mainMaxWait)) {
            datasource.setMaxWait(Integer.parseInt(mainMaxWait));
        }
        if (StringUtils.isNotBlank(mainTimeBetweenEvictionRunsMillis)) {
            datasource.setTimeBetweenEvictionRunsMillis(Integer.parseInt(mainTimeBetweenEvictionRunsMillis));
        }
        if (StringUtils.isNotBlank(mainMinEvictableIdleTimeMillis)) {
            datasource.setMinEvictableIdleTimeMillis(Integer.parseInt(mainMinEvictableIdleTimeMillis));
        }
        datasource.setValidationQuery(mainValidationQuery);
        datasource.setTestWhileIdle(true);
        datasource.setTestOnBorrow(false);
        datasource.setTestOnReturn(false);
        try {
            datasource.setFilters(mainFilters);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return datasource;
    }

    @Bean
    public ServletRegistrationBean statViewServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        servletRegistrationBean.addInitParameter("allow", "");
        servletRegistrationBean.addInitParameter("deny", "");
        servletRegistrationBean.addInitParameter("loginUsername", "root");
        servletRegistrationBean.addInitParameter("loginPassword", "root");
        servletRegistrationBean.addInitParameter("resetEnable", "false");
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean statFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
}